package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.Spot;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.query.SpotQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.repository.SpotRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.ISpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SpotServiceImpl implements ISpotService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/


    @Autowired
    private SpotRepository repository;
    @Autowired
    private MongoTemplate template;




    @Override
    public Spot get(String id) {
        Spot spot = repository.findById(id).get();
        return spot;
    }

    @Override
    public List<Spot> list() {
        List<Spot> spots = repository.findAll();
        return spots;
    }

    //分页查询
    @Override
    public Page<Spot> query(SpotQuery qo) {
        Query query=new Query();
        //repository只能针对所有数据进行分页
        //template不能直接对数据进行分页
        //使用template间接分页
        //pageResult 核心字段
        //totalCount 总记录数
        long totalcount = template.count(query, Spot.class);
        if (totalcount==0){
            //空的分页对象
            return Page.empty();
        }
        //list 当前页显示数据集合
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<Spot> list = template.find(query, Spot.class);
        return new PageImpl<>(list,pageable,totalcount);
    }


}
